<template>
  <li @mouseenter="dealShow(true)" @mouseleave="dealShow(false)" :style='{"backgroundColor": bgColor}'>
    <label>
      <input type="checkbox" v-model="todo.finished">
      <span> {{ index }} - {{todo.title}}</span>
    </label>
    <button v-show="isShowDelBtn" class="btn btn-warning" @click="delTodo">删除</button>
  </li>
</template>

<script>
export default {
  name: "Item",
  data() {
    return {
      isShowDelBtn: false,  // false 隐藏， true 显示
      bgColor: '#fff',
    }
  },
  props: {
    todo: Object,
    index: Number
  },
  methods: {
    dealShow(isShow){
      // 1. 控制按钮的显示与隐藏
      this.isShowDelBtn = isShow;

      // 2. 控制背景颜色
      this.bgColor = isShow ? '#ddd' : '#fff'
    },
    delTodo(){
      debugger
      // 将消息传递给上一层的 list，再有 list 传递给 app 组件
      this.$emit("btnDel", this.index);
    }
  }
}
</script>

<style scoped>
/* item */
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  margin-top: 3px;
  line-height: 14px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
</style>
